﻿using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using JelleDruyts.Windows.ObjectModel;

namespace Schedulr.ViewModels
{
    /// <summary>
    /// A list of <see cref="FlickrCollectionListItem"/> instances.
    /// </summary>
    public class FlickrCollectionList : ObservableObject
    {
        #region Fields

        private string descriptionPrefix;

        #endregion

        #region Properties

        /// <summary>
        /// Gets the items.
        /// </summary>
        public IList<FlickrCollectionListItem> Items { get; private set; }

        #endregion

        #region Observable Properties

        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        public string Description
        {
            get { return this.GetValue(DescriptionProperty); }
            set { this.SetValue(DescriptionProperty, value); }
        }

        /// <summary>
        /// The definition for the <see cref="Description"/> observable property.
        /// </summary>
        public static ObservableProperty<string> DescriptionProperty = new ObservableProperty<string, FlickrCollectionList>(o => o.Description);

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="FlickrCollectionList"/> class.
        /// </summary>
        /// <param name="items">The list items.</param>
        /// <param name="descriptionPrefix">The description prefix.</param>
        public FlickrCollectionList(IList<FlickrCollectionListItem> items, string descriptionPrefix)
        {
            this.descriptionPrefix = descriptionPrefix;
            if (items == null)
            {
                items = new List<FlickrCollectionListItem>();
            }
            this.Items = items;
            foreach (var item in this.Items)
            {
                // When a property of a list item changed, it's most probably the IsSelected property;
                // so update the description here.
                item.PropertyChanged += (sender, e) => SetDescription();
            }
            SetDescription();
        }

        #endregion

        #region Helper Methods

        /// <summary>
        /// Sets the description.
        /// </summary>
        private void SetDescription()
        {
            var selectedItemCount = this.Items.Count(i => i.IsSelected == true);
            var partiallySelectedItemCount = this.Items.Count(i => i.IsSelected == null);
            var partiallySelectedMessage = string.Empty;
            if (partiallySelectedItemCount > 0)
            {
                partiallySelectedMessage = string.Format(CultureInfo.CurrentCulture, " by all, {0} selected by some", partiallySelectedItemCount);
            }
            this.Description = string.Format(CultureInfo.CurrentCulture, "{0} ({1} selected{2})", this.descriptionPrefix, selectedItemCount, partiallySelectedMessage);
        }

        #endregion
    }
}